home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Python / structmember.c < prev    next >
C/C++ Source or Header  |  1999-04-25  |  7KB  |  285 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Map C struct members to Python object attributes */
  33.  
  34. #include "Python.h"
  35.  
  36. #include "structmember.h"
  37. #include "protos/structmember.h"
  38.  
  39. static PyObject *
  40. listmembers(mlist)
  41.     struct memberlist *mlist;
  42. {
  43.     int i, n;
  44.     PyObject *v;
  45.     for (n = 0; mlist[n].name != NULL; n++)
  46.         ;
  47.     v = PyList_New(n);
  48.     if (v != NULL) {
  49.         for (i = 0; i < n; i++)
  50.             PyList_SetItem(v, i,
  51.                        PyString_FromString(mlist[i].name));
  52.         if (PyErr_Occurred()) {
  53.             Py_DECREF(v);
  54.             v = NULL;
  55.         }
  56.         else {
  57.             PyList_Sort(v);
  58.         }
  59.     }
  60.     return v;
  61. }
  62.  
  63. PyObject *
  64. PyMember_Get(addr, mlist, name)
  65.     char *addr;
  66.     struct memberlist *mlist;
  67.     char *name;
  68. {
  69.     struct memberlist *l;
  70.     
  71.     if (strcmp(name, "__members__") == 0)
  72.         return listmembers(mlist);
  73.     for (l = mlist; l->name != NULL; l++) {
  74.         if (strcmp(l->name, name) == 0) {
  75.             PyObject *v;
  76.             addr += l->offset;
  77.             switch (l->type) {
  78.             case T_BYTE:
  79.                 v = PyInt_FromLong((long)
  80.                          (((*(char*)addr & 0xff)
  81.                            ^ 0x80) - 0x80));
  82.                 break;
  83.             case T_UBYTE:
  84.                 v = PyInt_FromLong((long) *(char*)addr & 0xff);
  85.                 break;
  86.             case T_SHORT:
  87.                 v = PyInt_FromLong((long) *(short*)addr);
  88.                 break;
  89.             case T_USHORT:
  90.                 v = PyInt_FromLong((long)
  91.                          *(unsigned short*)addr);
  92.                 break;
  93.             case T_INT:
  94.                 v = PyInt_FromLong((long) *(int*)addr);
  95.                 break;
  96.             case T_UINT:
  97.                 v = PyInt_FromLong((long)
  98.                            *(unsigned int*)addr);
  99.                 break;
  100.             case T_LONG:
  101.                 v = PyInt_FromLong(*(long*)addr);
  102.                 break;
  103.             case T_ULONG:
  104.                 v = PyLong_FromDouble((double)
  105.                            *(unsigned long*)addr);
  106.                 break;
  107.             case T_FLOAT:
  108.                 v = PyFloat_FromDouble((double)*(float*)addr);
  109.                 break;
  110.             case T_DOUBLE:
  111.                 v = PyFloat_FromDouble(*(double*)addr);
  112.                 break;
  113.             case T_STRING:
  114.                 if (*(char**)addr == NULL) {
  115.                     Py_INCREF(Py_None);
  116.                     v = Py_None;
  117.                 }
  118.                 else
  119.                     v = PyString_FromString(*(char**)addr);
  120.                 break;
  121.             case T_STRING_INPLACE:
  122.                 v = PyString_FromString((char*)addr);
  123.                 break;
  124. #ifdef macintosh
  125.             case T_PSTRING:
  126.                 if (*(char**)addr == NULL) {
  127.                     Py_INCREF(Py_None);
  128.                     v = Py_None;
  129.                 }
  130.                 else
  131.                     v = PyString_FromStringAndSize(
  132.                         (*(char**)addr)+1,
  133.                         **(unsigned char**)addr);
  134.                 break;
  135.             case T_PSTRING_INPLACE:
  136.                 v = PyString_FromStringAndSize(
  137.                     ((char*)addr)+1,
  138.                     *(unsigned char*)addr);
  139.                 break;
  140. #endif /* macintosh */
  141.             case T_CHAR:
  142.                 v = PyString_FromStringAndSize((char*)addr, 1);
  143.                 break;
  144.             case T_OBJECT:
  145.                 v = *(PyObject **)addr;
  146.                 if (v == NULL)
  147.                     v = Py_None;
  148.                 Py_INCREF(v);
  149.                 break;
  150.             default:
  151.                 PyErr_SetString(PyExc_SystemError,
  152.                         "bad memberlist type");
  153.                 v = NULL;
  154.             }
  155.             return v;
  156.         }
  157.     }
  158.     
  159.     PyErr_SetString(PyExc_AttributeError, name);
  160.     return NULL;
  161. }
  162.  
  163. int
  164. PyMember_Set(addr, mlist, name, v)
  165.     char *addr;
  166.     struct memberlist *mlist;
  167.     char *name;
  168.     PyObject *v;
  169. {
  170.     struct memberlist *l;
  171.     PyObject *oldv;
  172.     
  173.     for (l = mlist; l->name != NULL; l++) {
  174.         if (strcmp(l->name, name) == 0) {
  175. #ifdef macintosh
  176.             if (l->readonly || l->type == T_STRING ||
  177.                 l->type == T_PSTRING)
  178.             {
  179. #else
  180.             if (l->readonly || l->type == T_STRING ) {
  181. #endif /* macintosh */
  182.                 PyErr_SetString(PyExc_TypeError,
  183.                         "readonly attribute");
  184.                 return -1;
  185.             }
  186.             if (v == NULL && l->type != T_OBJECT) {
  187.                 PyErr_SetString(PyExc_TypeError,
  188.                   "can't delete numeric/char attribute");
  189.                 return -1;
  190.             }
  191.             addr += l->offset;
  192.             switch (l->type) {
  193.             case T_BYTE:
  194.             case T_UBYTE:
  195.                 if (!PyInt_Check(v)) {
  196.                     PyErr_BadArgument();
  197.                     return -1;
  198.                 }
  199.                 *(char*)addr = (char) PyInt_AsLong(v);
  200.                 break;
  201.             case T_SHORT:
  202.             case T_USHORT:
  203.                 if (!PyInt_Check(v)) {
  204.                     PyErr_BadArgument();
  205.                     return -1;
  206.                 }
  207.                 *(short*)addr = (short) PyInt_AsLong(v);
  208.                 break;
  209.             case T_UINT:
  210.             case T_INT:
  211.                 if (!PyInt_Check(v)) {
  212.                     PyErr_BadArgument();
  213.                     return -1;
  214.                 }
  215.                 *(int*)addr = (int) PyInt_AsLong(v);
  216.                 break;
  217.             case T_LONG:
  218.                 if (!PyInt_Check(v)) {
  219.                     PyErr_BadArgument();
  220.                     return -1;
  221.                 }
  222.                 *(long*)addr = PyInt_AsLong(v);
  223.                 break;
  224.             case T_ULONG:
  225.                 if (PyInt_Check(v))
  226.                     *(long*)addr = PyInt_AsLong(v);
  227.                 else if (PyLong_Check(v))
  228.                     *(long*)addr = PyLong_AsLong(v);
  229.                 else {
  230.                     PyErr_BadArgument();
  231.                     return -1;
  232.                 }
  233.                 break;
  234.             case T_FLOAT:
  235.                 if (PyInt_Check(v))
  236.                     *(float*)addr =
  237.                         (float) PyInt_AsLong(v);
  238.                 else if (PyFloat_Check(v))
  239.                     *(float*)addr =
  240.                         (float) PyFloat_AsDouble(v);
  241.                 else {
  242.                     PyErr_BadArgument();
  243.                     return -1;
  244.                 }
  245.                 break;
  246.             case T_DOUBLE:
  247.                 if (PyInt_Check(v))
  248.                     *(double*)addr =
  249.                         (double) PyInt_AsLong(v);
  250.                 else if (PyFloat_Check(v))
  251.                     *(double*)addr = PyFloat_AsDouble(v);
  252.                 else {
  253.                     PyErr_BadArgument();
  254.                     return -1;
  255.                 }
  256.                 break;
  257.             case T_OBJECT:
  258.                 Py_XINCREF(v);
  259.                 oldv = *(PyObject **)addr;
  260.                 *(PyObject **)addr = v;
  261.                 Py_XDECREF(oldv);
  262.                 break;
  263.             case T_CHAR:
  264.                 if (PyString_Check(v) &&
  265.                     PyString_Size(v) == 1) {
  266.                     *(char*)addr =
  267.                         PyString_AsString(v)[0];
  268.                 }
  269.                 else {
  270.                     PyErr_BadArgument();
  271.                     return -1;
  272.                 }
  273.             default:
  274.                 PyErr_SetString(PyExc_SystemError,
  275.                         "bad memberlist type");
  276.                 return -1;
  277.             }
  278.             return 0;
  279.         }
  280.     }
  281.     
  282.     PyErr_SetString(PyExc_AttributeError, name);
  283.     return -1;
  284. }
  285.